package com.k.comnote.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.k.comnote.R;
import com.k.comnote.adapter.MyAdapterForRecent;
import com.k.comnote.database.NoteDB;
import com.k.comnote.model.ItemNoteRecent;
import com.k.comnote.model.Note;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by K on 2015/8/17.
 */
public class Fragment_1 extends Fragment {

    private List<Note> noteRecentList;
    private List<Note> noteRecentListResume;
    private List<ItemNoteRecent> noteItemNoteRecentList;
    private MyAdapterForRecent myAdapterForRecent;
    private ListView listView;
    private NoteDB db;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, null);
        listView = (ListView) view.findViewById(R.id.note_recently);
        db = NoteDB.getNoteDB(getActivity());
        noteRecentList = db.loadNote();
        Log.d("saveNoteLogList", noteRecentList.size() + "");

        Log.d("生命周期", "in the onCreateView");
        noteItemNoteRecentList = new ArrayList<>();
        if (noteRecentList.size() == 0) {
            Note note = new Note();
            note.setTitle("日记本空空如也");
            note.setTime("那些逝去的日子");
            note.setContent("       即使时光不等人，也总有小笔记陪你一同记录过去的点滴美好");
            noteRecentList.add(note);

        }
        for (Note note : noteRecentList) {
            noteItemNoteRecentList.add(new ItemNoteRecent(note.getTitle(), note.getTime(), note.getContent()));
        }

        myAdapterForRecent = new MyAdapterForRecent(getActivity(), noteItemNoteRecentList);
        listView.setAdapter(myAdapterForRecent);
        myAdapterForRecent.notifyDataSetChanged();
        /*mCallback = new ModeCallback();
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(mCallback);*/

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setIcon(R.drawable.alert);
                builder.setTitle("提示：")
                        .setMessage("确定删除该条日记？")
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("alertLog: 取消   ", which + "");//-1
                            }
                        })

                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("alertLog: 确定    ", which + "      " + position + "      id:" + id);//-2
                                //listView.removeViewAt(position);

                                Note note = noteRecentList.get(position);
                                Log.d("get Note :", note.getTitle() + "   " + note.getTime());
                                db.deleteNote(note);
                                noteItemNoteRecentList.remove(position);
                                myAdapterForRecent.notifyDataSetChanged();

                            }
                        });
                builder.create();
                builder.show();
                return true;
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NoteShow.class);
                Bundle bundle = new Bundle();
                bundle.putString("title", noteRecentList.get(position).getTitle());
                bundle.putString("time", noteRecentList.get(position).getTime());
                bundle.putString("content", noteRecentList.get(position).getContent());

                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("生命周期", "in the onResume");
        noteRecentListResume = db.loadNote();
        if (noteRecentList.size() != noteRecentListResume.size() && noteRecentListResume.size() != 0) {
            Note note = noteRecentListResume.get(noteRecentListResume.size()-1);
            noteRecentList.add(note);
            noteItemNoteRecentList.add(new ItemNoteRecent(note.getTitle(), note.getTime(), note.getContent()));
            myAdapterForRecent.notifyDataSetChanged();
        }

    }

  /*  private class ModeCallback implements ListView.MultiChoiceModeListener {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.menu_cover, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.ac_bar_delete_note:

            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    }
*/

}
